﻿using System;
using System.Collections.Generic;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;

namespace Model {

  public class Uf {

    private String codUf;
    private String nome;
    private String idUF;

    #region Propriedades
    public String IdUF
    {
        get { return idUF; }
        set { idUF = value; }
    }

    public String Nome {
      get { return nome; }
      set { nome = value; }
    }

    public string CodUf
    {
        get { return codUf; }
        set { codUf = value; }
    }
    #endregion

    public Uf()
    {
    }

    public Uf(string pIdUf, string pNome, string pCodUF)
    {
        this.IdUF = pIdUf;
        this.Nome = pNome;
        this.CodUf = pCodUF;
    }

    #region Metodos

    #region Persist
    public Boolean Persist()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Save(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Update
    public Boolean Update()
    {
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objSession.Update(this);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region Delete
    public static Boolean Delete(int pIdUF)
    {
        Uf objUf = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objUf = (Uf)objSession.Load(typeof(Model.Uf), pIdUF);
            objSession.Delete(objUf);
            objTransaction.Commit();
            objSession.Close();
            return true;
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
    }
    #endregion

    #region RetriveObject
    public static Uf RetriveObject(string pIdUF)
    {
        Uf objUF = null;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            objUF = (Uf)objSession.Load(typeof(Model.Uf), pIdUF);
            objSession.Close();
        }
        catch (Exception ex)
        {
            objTransaction.Rollback();
            objSession.Close();
            throw ex;
        }
        return objUF;
    }
    #endregion

    #region RetriveObjects
    public static IList RetriveObjects()
    {
        IList listUFs;
        ISession objSession = ConnectionFactory.getConnection().OpenSession();
        ITransaction objTransaction = objSession.BeginTransaction();

        try
        {
            listUFs = objSession.CreateCriteria(typeof(Model.Uf)).List();
            objSession.Close();

        }
        catch (Exception ex)
        {
            listUFs = null;
            objTransaction.Rollback();
            throw ex;
        }
        return listUFs;
    }
    #endregion

    #endregion

  }
}
